package com.stepforward.me;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;



public class AddFavorite extends Activity{
    private static final int REQUEST_CODE = 1234;
	private EditText txtDep;
	private EditText txtArr;
	private EditText txtName;
	private TextView txtFav;
	MyFavoriteHelper dbHelper;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        dbHelper=new MyFavoriteHelper(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add);
        txtDep=(EditText)findViewById(R.id.Dep);
        txtArr=(EditText)findViewById(R.id.Arr);
        txtName=(EditText)findViewById(R.id.Name);
        txtFav=(TextView)findViewById(R.id.Fav);
        txtFav.setText(txtFav.getText()+String.valueOf(dbHelper.getFavCount()));
        Bundle extras = getIntent().getExtras();
		String flag=extras.getString("flag");
        if(flag.equals("true"))
        {
		
		String departure= extras.getString("departure");
		String arrival= extras.getString("arrival");
			txtDep.setText(departure);
			txtArr.setText(arrival);
			
		}
        
        }
    @Override
	public void onStart()
	{
		try
		{
		super.onStart();

	}
		catch(Exception ex)
		{
		CatchError(ex.toString());
		}
	}

	void CatchError(String Exception)
	{
		Dialog diag=new Dialog(this);
		diag.setTitle("Message");
		TextView txt=new TextView(this);
		txt.setText(Exception);
		diag.setContentView(txt);
		diag.show();
	}
	public void btnAddRoute_Click(View view)
	{
		boolean ok=true;
		try
		{
	
		String arr=	txtArr.getText().toString();
		String dep=	txtDep.getText().toString();
		String name=txtName.getText().toString();
		myFavorite myfav=new myFavorite(dep, arr, name);
		ok=dbHelper.insertFav(myfav);
		}
		catch(Exception ex)
		{
			ok=false;
			CatchError(ex.toString());
			
		}
		finally
		{
			if(ok)
			{
				
				Alerts.ShowAddedAlert(this);
				txtFav.setText("Number of favorites "+String.valueOf(dbHelper.getFavCount()));
				txtName.setText("");
				txtArr.setText("");
				txtDep.setText("");
			}
			else
			{
				CatchError("Please rename it, the name has already been used");
				
			}
		}
		
		
	}
	
}
